unit WinForm;

interface

uses
  System.Drawing, System.Collections, System.ComponentModel,
  System.Windows.Forms, System.Data,System.Data.SqlClient;

type
  TWinForm = class(System.Windows.Forms.Form)
  {$REGION 'Designer Managed Code'}
  strict private
    /// <summary>
    /// Required designer variable.
    /// </summary>
    Components: System.ComponentModel.Container;
    TabControl1: System.Windows.Forms.TabControl;
    TabPage1: System.Windows.Forms.TabPage;
    TabPage2: System.Windows.Forms.TabPage;
    TabPage3: System.Windows.Forms.TabPage;
    TabPage4: System.Windows.Forms.TabPage;
    Button0: System.Windows.Forms.Button;
    DataGrid1: System.Windows.Forms.DataGrid;
    Button1: System.Windows.Forms.Button;
    TextBox1: System.Windows.Forms.TextBox;
    TextBox2: System.Windows.Forms.TextBox;
    TextBox3: System.Windows.Forms.TextBox;
    TextBox4: System.Windows.Forms.TextBox;
    TextBox5: System.Windows.Forms.TextBox;
    TextBox6: System.Windows.Forms.TextBox;
    Label1: System.Windows.Forms.Label;
    Label2: System.Windows.Forms.Label;
    Label3: System.Windows.Forms.Label;
    Label4: System.Windows.Forms.Label;
    Label5: System.Windows.Forms.Label;
    Label6: System.Windows.Forms.Label;
    TextBox8: System.Windows.Forms.TextBox;
    Label7: System.Windows.Forms.Label;
    DataGrid2: System.Windows.Forms.DataGrid;
    Button2: System.Windows.Forms.Button;
    Label8: System.Windows.Forms.Label;
    TextBox7: System.Windows.Forms.TextBox;
    Button5: System.Windows.Forms.Button;
    DataGrid3: System.Windows.Forms.DataGrid;
    Button6: System.Windows.Forms.Button;
    Button8: System.Windows.Forms.Button;
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    procedure InitializeComponent;
    procedure Button1_Click(sender: System.Object; e: System.EventArgs);
    procedure Button1_Click1(sender: System.Object; e: System.EventArgs);
    procedure Button2_Click(sender: System.Object; e: System.EventArgs);
    procedure Button5_Click(sender: System.Object; e: System.EventArgs);
    procedure TabPage3_Click(sender: System.Object; e: System.EventArgs);
    procedure Button6_Click(sender: System.Object; e: System.EventArgs);
    procedure Button8_Click(sender: System.Object; e: System.EventArgs);

  {$ENDREGION}
  strict protected
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    procedure Dispose(Disposing: Boolean); override;
  private
    { Private Declarations }
    var myConnection:SqlConnection;
        myCommand:SqlCommand;
        myDataAdapter:SqlDataAdapter;
        myDataSet:DataSet;
        myDataRow:DataRow;
        mySqlParameter: SqlParameter;
  public
    constructor Create;
  end;

  [assembly: RuntimeRequiredAttribute(TypeOf(TWinForm))]

implementation

{$AUTOBOX ON}

{$REGION 'Windows Form Designer generated code'}
/// <summary>
/// Required method for Designer support -- do not modify
/// the contents of this method with the code editor.
/// </summary>
procedure TWinForm.InitializeComponent;
begin
  Self.TabControl1 := System.Windows.Forms.TabControl.Create;
  Self.TabPage1 := System.Windows.Forms.TabPage.Create;
  Self.DataGrid1 := System.Windows.Forms.DataGrid.Create;
  Self.Button0 := System.Windows.Forms.Button.Create;
  Self.TabPage2 := System.Windows.Forms.TabPage.Create;
  Self.TextBox7 := System.Windows.Forms.TextBox.Create;
  Self.Label8 := System.Windows.Forms.Label.Create;
  Self.Label7 := System.Windows.Forms.Label.Create;
  Self.TextBox8 := System.Windows.Forms.TextBox.Create;
  Self.Label6 := System.Windows.Forms.Label.Create;
  Self.Label5 := System.Windows.Forms.Label.Create;
  Self.Label4 := System.Windows.Forms.Label.Create;
  Self.Label3 := System.Windows.Forms.Label.Create;
  Self.Label2 := System.Windows.Forms.Label.Create;
  Self.Label1 := System.Windows.Forms.Label.Create;
  Self.TextBox6 := System.Windows.Forms.TextBox.Create;
  Self.TextBox5 := System.Windows.Forms.TextBox.Create;
  Self.TextBox4 := System.Windows.Forms.TextBox.Create;
  Self.TextBox3 := System.Windows.Forms.TextBox.Create;
  Self.TextBox2 := System.Windows.Forms.TextBox.Create;
  Self.TextBox1 := System.Windows.Forms.TextBox.Create;
  Self.Button1 := System.Windows.Forms.Button.Create;
  Self.TabPage3 := System.Windows.Forms.TabPage.Create;
  Self.Button5 := System.Windows.Forms.Button.Create;
  Self.Button2 := System.Windows.Forms.Button.Create;
  Self.DataGrid2 := System.Windows.Forms.DataGrid.Create;
  Self.TabPage4 := System.Windows.Forms.TabPage.Create;
  Self.Button8 := System.Windows.Forms.Button.Create;
  Self.Button6 := System.Windows.Forms.Button.Create;
  Self.DataGrid3 := System.Windows.Forms.DataGrid.Create;
  Self.TabControl1.SuspendLayout;
  Self.TabPage1.SuspendLayout;
  (System.ComponentModel.ISupportInitialize(Self.DataGrid1)).BeginInit;
  Self.TabPage2.SuspendLayout;
  Self.TabPage3.SuspendLayout;
  (System.ComponentModel.ISupportInitialize(Self.DataGrid2)).BeginInit;
  Self.TabPage4.SuspendLayout;
  (System.ComponentModel.ISupportInitialize(Self.DataGrid3)).BeginInit;
  Self.SuspendLayout;
  // 
  // TabControl1
  // 
  Self.TabControl1.Controls.Add(Self.TabPage1);
  Self.TabControl1.Controls.Add(Self.TabPage2);
  Self.TabControl1.Controls.Add(Self.TabPage3);
  Self.TabControl1.Controls.Add(Self.TabPage4);
  Self.TabControl1.Dock := System.Windows.Forms.DockStyle.Fill;
  Self.TabControl1.Location := System.Drawing.Point.Create(0, 0);
  Self.TabControl1.Name := 'TabControl1';
  Self.TabControl1.SelectedIndex := 0;
  Self.TabControl1.Size := System.Drawing.Size.Create(664, 358);
  Self.TabControl1.TabIndex := 0;
  // 
  // TabPage1
  // 
  Self.TabPage1.Controls.Add(Self.DataGrid1);
  Self.TabPage1.Controls.Add(Self.Button0);
  Self.TabPage1.Location := System.Drawing.Point.Create(4, 21);
  Self.TabPage1.Name := 'TabPage1';
  Self.TabPage1.Size := System.Drawing.Size.Create(656, 333);
  Self.TabPage1.TabIndex := 0;
  Self.TabPage1.Text := 'ʾ';
  // 
  // DataGrid1
  // 
  Self.DataGrid1.DataMember := '';
  Self.DataGrid1.HeaderForeColor := System.Drawing.SystemColors.ControlText;
  Self.DataGrid1.Location := System.Drawing.Point.Create(8, 8);
  Self.DataGrid1.Name := 'DataGrid1';
  Self.DataGrid1.ReadOnly := True;
  Self.DataGrid1.Size := System.Drawing.Size.Create(640, 272);
  Self.DataGrid1.TabIndex := 1;
  // 
  // Button0
  // 
  Self.Button0.Location := System.Drawing.Point.Create(288, 296);
  Self.Button0.Name := 'Button0';
  Self.Button0.TabIndex := 0;
  Self.Button0.Text := '   ʾ';
  Include(Self.Button0.Click, Self.Button1_Click);
  // 
  // TabPage2
  // 
  Self.TabPage2.Controls.Add(Self.TextBox7);
  Self.TabPage2.Controls.Add(Self.Label8);
  Self.TabPage2.Controls.Add(Self.Label7);
  Self.TabPage2.Controls.Add(Self.TextBox8);
  Self.TabPage2.Controls.Add(Self.Label6);
  Self.TabPage2.Controls.Add(Self.Label5);
  Self.TabPage2.Controls.Add(Self.Label4);
  Self.TabPage2.Controls.Add(Self.Label3);
  Self.TabPage2.Controls.Add(Self.Label2);
  Self.TabPage2.Controls.Add(Self.Label1);
  Self.TabPage2.Controls.Add(Self.TextBox6);
  Self.TabPage2.Controls.Add(Self.TextBox5);
  Self.TabPage2.Controls.Add(Self.TextBox4);
  Self.TabPage2.Controls.Add(Self.TextBox3);
  Self.TabPage2.Controls.Add(Self.TextBox2);
  Self.TabPage2.Controls.Add(Self.TextBox1);
  Self.TabPage2.Controls.Add(Self.Button1);
  Self.TabPage2.Location := System.Drawing.Point.Create(4, 21);
  Self.TabPage2.Name := 'TabPage2';
  Self.TabPage2.Size := System.Drawing.Size.Create(656, 333);
  Self.TabPage2.TabIndex := 1;
  Self.TabPage2.Text := '';
  // 
  // TextBox7
  // 
  Self.TextBox7.Location := System.Drawing.Point.Create(368, 112);
  Self.TextBox7.Name := 'TextBox7';
  Self.TextBox7.TabIndex := 16;
  Self.TextBox7.Text := '60';
  // 
  // Label8
  // 
  Self.Label7.Font := System.Drawing.Font.Create('', 10, System.Drawing.FontStyle.Regular, 
      System.Drawing.GraphicsUnit.Point, (Byte(134)));
  Self.Label7.Location := System.Drawing.Point.Create(328, 117);
  Self.Label7.Name := 'Label8';
  Self.Label7.Size := System.Drawing.Size.Create(40, 23);
  Self.Label7.TabIndex := 15;
  Self.Label7.Text := 'ɼ';
  // 
  // Label7
  // 
  Self.Label7.Font := System.Drawing.Font.Create('', 10, System.Drawing.FontStyle.Regular, 
      System.Drawing.GraphicsUnit.Point, (Byte(134)));
  Self.Label7.Location := System.Drawing.Point.Create(64, 166);
  Self.Label7.Name := 'Label7';
  Self.Label7.TabIndex := 14;
  Self.Label7.Text := 'ע';
  // 
  // TextBox8
  // 
  Self.TextBox7.Location := System.Drawing.Point.Create(64, 190);
  Self.TextBox7.Multiline := True;
  Self.TextBox7.Name := 'TextBox8';
  Self.TextBox7.Size := System.Drawing.Size.Create(440, 120);
  Self.TextBox7.TabIndex := 13;
  Self.TextBox7.Text := 'ͽľ';
  // 
  // Label6
  // 
  Self.Label6.Font := System.Drawing.Font.Create('', 10, System.Drawing.FontStyle.Regular, 
      System.Drawing.GraphicsUnit.Point, (Byte(134)));
  Self.Label6.Location := System.Drawing.Point.Create(328, 67);
  Self.Label6.Name := 'Label6';
  Self.Label6.Size := System.Drawing.Size.Create(40, 23);
  Self.Label6.TabIndex := 12;
  Self.Label6.Text := 'רҵ';
  // 
  // Label5
  // 
  Self.Label5.Font := System.Drawing.Font.Create('', 10, System.Drawing.FontStyle.Regular, 
      System.Drawing.GraphicsUnit.Point, (Byte(134)));
  Self.Label5.Location := System.Drawing.Point.Create(320, 20);
  Self.Label5.Name := 'Label5';
  Self.Label5.Size := System.Drawing.Size.Create(48, 23);
  Self.Label5.TabIndex := 11;
  Self.Label5.Text := ' ';
  // 
  // Label4
  // 
  Self.Label4.Font := System.Drawing.Font.Create('', 10, System.Drawing.FontStyle.Regular, 
      System.Drawing.GraphicsUnit.Point, (Byte(134)));
  Self.Label4.Location := System.Drawing.Point.Create(29, 123);
  Self.Label4.Name := 'Label4';
  Self.Label4.Size := System.Drawing.Size.Create(64, 23);
  Self.Label4.TabIndex := 10;
  Self.Label4.Text := '';
  // 
  // Label3
  // 
  Self.Label3.Font := System.Drawing.Font.Create('', 10, System.Drawing.FontStyle.Regular, 
      System.Drawing.GraphicsUnit.Point, (Byte(134)));
  Self.Label3.Location := System.Drawing.Point.Create(56, 88);
  Self.Label3.Name := 'Label3';
  Self.Label3.Size := System.Drawing.Size.Create(40, 23);
  Self.Label3.TabIndex := 9;
  Self.Label3.Text := 'Ա';
  // 
  // Label2
  // 
  Self.Label2.Font := System.Drawing.Font.Create('', 10, System.Drawing.FontStyle.Regular, 
      System.Drawing.GraphicsUnit.Point, (Byte(134)));
  Self.Label2.Location := System.Drawing.Point.Create(56, 54);
  Self.Label2.Name := 'Label2';
  Self.Label2.Size := System.Drawing.Size.Create(32, 23);
  Self.Label2.TabIndex := 8;
  Self.Label2.Text := '';
  // 
  // Label1
  // 
  Self.Label1.Font := System.Drawing.Font.Create('', 10, System.Drawing.FontStyle.Regular, 
      System.Drawing.GraphicsUnit.Point, (Byte(134)));
  Self.Label1.Location := System.Drawing.Point.Create(57, 20);
  Self.Label1.Name := 'Label1';
  Self.Label1.Size := System.Drawing.Size.Create(32, 23);
  Self.Label1.TabIndex := 7;
  Self.Label1.Text := 'ѧ';
  // 
  // TextBox6
  // 
  Self.TextBox6.Location := System.Drawing.Point.Create(368, 63);
  Self.TextBox6.Name := 'TextBox6';
  Self.TextBox6.TabIndex := 6;
  Self.TextBox6.Text := 'Ӧ';
  // 
  // TextBox5
  // 
  Self.TextBox5.Location := System.Drawing.Point.Create(368, 15);
  Self.TextBox5.Name := 'TextBox5';
  Self.TextBox5.TabIndex := 5;
  Self.TextBox5.Text := '䵱ɽ';
  // 
  // TextBox4
  // 
  Self.TextBox4.Location := System.Drawing.Point.Create(96, 120);
  Self.TextBox4.Name := 'TextBox4';
  Self.TextBox4.TabIndex := 4;
  Self.TextBox4.Text := '05/06/1978';
  // 
  // TextBox3
  // 
  Self.TextBox3.Location := System.Drawing.Point.Create(96, 85);
  Self.TextBox3.Name := 'TextBox3';
  Self.TextBox3.TabIndex := 3;
  Self.TextBox3.Text := '1';
  // 
  // TextBox2
  // 
  Self.TextBox2.Location := System.Drawing.Point.Create(96, 50);
  Self.TextBox2.Name := 'TextBox2';
  Self.TextBox2.TabIndex := 2;
  Self.TextBox2.Text := '';
  // 
  // TextBox1
  // 
  Self.TextBox1.Location := System.Drawing.Point.Create(96, 15);
  Self.TextBox1.Name := 'TextBox1';
  Self.TextBox1.TabIndex := 1;
  Self.TextBox1.Text := '0538010113';
  // 
  // Button1
  // 
  Self.Button1.Location := System.Drawing.Point.Create(560, 288);
  Self.Button1.Name := 'Button1';
  Self.Button1.TabIndex := 0;
  Self.Button1.Text := '';
  Include(Self.Button1.Click, Self.Button1_Click1);
  // 
  // TabPage3
  // 
  Self.TabPage3.Controls.Add(Self.Button5);
  Self.TabPage3.Controls.Add(Self.Button2);
  Self.TabPage3.Controls.Add(Self.DataGrid2);
  Self.TabPage3.Location := System.Drawing.Point.Create(4, 21);
  Self.TabPage3.Name := 'TabPage3';
  Self.TabPage3.Size := System.Drawing.Size.Create(656, 333);
  Self.TabPage3.TabIndex := 2;
  Self.TabPage3.Text := '޸';
  Include(Self.TabPage3.Click, Self.TabPage3_Click);
  // 
  // Button5
  // 
  Self.Button5.Location := System.Drawing.Point.Create(424, 304);
  Self.Button5.Name := 'Button5';
  Self.Button5.TabIndex := 4;
  Self.Button5.Text := '޸';
  Include(Self.Button5.Click, Self.Button5_Click);
  // 
  // Button2
  // 
  Self.Button2.Location := System.Drawing.Point.Create(184, 304);
  Self.Button2.Name := 'Button2';
  Self.Button2.TabIndex := 1;
  Self.Button2.Text := '';
  Include(Self.Button2.Click, Self.Button2_Click);
  // 
  // DataGrid2
  // 
  Self.DataGrid2.DataMember := '';
  Self.DataGrid2.HeaderForeColor := System.Drawing.SystemColors.ControlText;
  Self.DataGrid2.Location := System.Drawing.Point.Create(8, 8);
  Self.DataGrid2.Name := 'DataGrid2';
  Self.DataGrid2.Size := System.Drawing.Size.Create(640, 280);
  Self.DataGrid2.TabIndex := 0;
  // 
  // TabPage4
  // 
  Self.TabPage4.Controls.Add(Self.Button8);
  Self.TabPage4.Controls.Add(Self.Button6);
  Self.TabPage4.Controls.Add(Self.DataGrid3);
  Self.TabPage4.Location := System.Drawing.Point.Create(4, 21);
  Self.TabPage4.Name := 'TabPage4';
  Self.TabPage4.Size := System.Drawing.Size.Create(656, 333);
  Self.TabPage4.TabIndex := 3;
  Self.TabPage4.Text := 'ɾ';
  // 
  // Button8
  // 
  Self.Button7.Location := System.Drawing.Point.Create(440, 296);
  Self.Button7.Name := 'Button8';
  Self.Button7.TabIndex := 4;
  Self.Button7.Text := 'ɾǰ';
  Include(Self.Button7.Click, Self.Button8_Click);
  // 
  // Button6
  // 
  Self.Button6.Location := System.Drawing.Point.Create(160, 296);
  Self.Button6.Name := 'Button6';
  Self.Button6.TabIndex := 1;
  Self.Button6.Text := '';
  Include(Self.Button6.Click, Self.Button6_Click);
  // 
  // DataGrid3
  // 
  Self.DataGrid3.DataMember := '';
  Self.DataGrid3.HeaderForeColor := System.Drawing.SystemColors.ControlText;
  Self.DataGrid3.Location := System.Drawing.Point.Create(8, 8);
  Self.DataGrid3.Name := 'DataGrid3';
  Self.DataGrid3.ReadOnly := True;
  Self.DataGrid3.Size := System.Drawing.Size.Create(640, 272);
  Self.DataGrid3.TabIndex := 0;
  // 
  // TWinForm
  // 
  Self.AutoScaleBaseSize := System.Drawing.Size.Create(6, 14);
  Self.ClientSize := System.Drawing.Size.Create(664, 358);
  Self.Controls.Add(Self.TabControl1);
  Self.Name := 'TWinForm';
  Self.Text := 'WinForm';
  Self.TabControl1.ResumeLayout(False);
  Self.TabPage1.ResumeLayout(False);
  (System.ComponentModel.ISupportInitialize(Self.DataGrid1)).EndInit;
  Self.TabPage2.ResumeLayout(False);
  Self.TabPage3.ResumeLayout(False);
  (System.ComponentModel.ISupportInitialize(Self.DataGrid2)).EndInit;
  Self.TabPage4.ResumeLayout(False);
  (System.ComponentModel.ISupportInitialize(Self.DataGrid3)).EndInit;
  Self.ResumeLayout(False);
end;
{$ENDREGION}

procedure TWinForm.Dispose(Disposing: Boolean);
begin
  if Disposing then
  begin
    if Components <> nil then
      Components.Dispose();
  end;
  inherited Dispose(Disposing);
end;

constructor TWinForm.Create;
begin
  inherited Create;
  //
  // Required for Windows Form Designer support
  //
  InitializeComponent;
  //
  // TODO: Add any constructor code after InitializeComponent call
  //
end;

procedure TWinForm.Button8_Click(sender: System.Object; e: System.EventArgs);
var SqlStr,cellValue:string;
    rowIndex:integer;
begin
    rowIndex := DataGrid3.CurrentRowIndex;
    cellValue := DataGrid3.Item[rowIndex,0].ToString;
    myCommand :=  myConnection.CreateCommand;
    SqlStr:= 'delete from stu where stu_id = '''+ cellValue +'''';
    myCommand.CommandText := SqlStr;
    myCommand.ExecuteNonQuery;    
    myDataSet := DataSet.Create;
    myDataAdapter.Fill(myDataSet,'stuTable');
     DataGrid3.DataSource := myDataSet.Tables.Item['stuTable'];  
end;

procedure TWinForm.Button6_Click(sender: System.Object; e: System.EventArgs);
begin
     myConnection := SqlConnection.Create('integrated security=SSPI;data source=localhost;persist security info=False;initial catalog=MyDatabase');
     myDataAdapter := SqlDataAdapter.Create('select * from stu',myConnection);
     myConnection.Open;
     myDataSet := DataSet.Create;
     myDataAdapter.Fill(myDataSet,'stuTable');
     DataGrid3.DataSource := myDataSet.Tables.Item['stuTable'];
end;

procedure TWinForm.Button5_Click(sender: System.Object; e: System.EventArgs);
begin
    myDataAdapter.Update(myDataSet.Tables.Item['stuTable']);
end; 

procedure TWinForm.Button2_Click(sender: System.Object; e: System.EventArgs);
begin
     myConnection := SqlConnection.Create('integrated security=SSPI;data source=localhost;persist security info=False;initial catalog=MyDatabase');
     myDataAdapter := SqlDataAdapter.Create('select stu_id, name, speciality from stu',myConnection);
     myDataAdapter.UpdateCommand  := SqlCommand.Create('update stu set name = @name,speciality = @speciality where stu_id = @stu_id', myConnection);
     myDataAdapter.UpdateCommand.Parameters.Add('@name', SqlDbType.NVarChar, 6, 'name');
     myDataAdapter.UpdateCommand.Parameters.Add('@speciality', SqlDbType.NVarChar, 50, 'speciality');
     mySqlParameter := myDataAdapter.UpdateCommand.Parameters.Add('@stu_id', SqlDbType.Int);
     mySqlParameter.SourceColumn := 'stu_id';
     mySqlParameter.SourceVersion := DataRowVersion.Original;
     myConnection.Open;
     myDataSet := DataSet.Create;
     myDataAdapter.Fill(myDataSet,'stuTable');
     DataGrid2.DataSource := myDataSet.Tables.Item['stuTable'];
    myConnection.close;
end;

procedure TWinForm.Button1_Click1(sender: System.Object; e: System.EventArgs);
var SqlStr:string;
begin
     myConnection := SqlConnection.Create('Data Source = localhost; Integrated Security = SSPI; Initial Catalog = MyDatabase');
     myCommand :=  myConnection.CreateCommand;
     SqlStr:= 'Insert into stu values('+TextBox1.Text +', '''+ TextBox2.Text +''', '+ TextBox3.Text +', '''+ TextBox4.Text +''','''+ TextBox5.Text +''', '''+ TextBox6.Text +''','+ TextBox7.Text +','''+ TextBox7.Text +''')';
     myCommand.CommandText := SqlStr;
     myConnection.Open;
     myCommand.ExecuteNonQuery;
     myConnection.Close;
end;

procedure TWinForm.Button1_Click(sender: System.Object; e: System.EventArgs);
begin
     myConnection := SqlConnection.Create('integrated security=SSPI;data source=localhost;persist security info=False;initial catalog=MyDatabase');
     myCommand := SqlCommand.Create('select * from stu');
     myDataAdapter := SqlDataAdapter.Create;
     myDataAdapter.SelectCommand := myCommand;
     myDataAdapter.SelectCommand.Connection := myConnection;
     myConnection.Open;
     myDataSet := DataSet.Create;
     myDataAdapter.Fill(myDataSet,'stuTable');    
     DataGrid1.DataSource := myDataSet.Tables.Item['stuTable'];
    myConnection.close;
end;
end.
